package com.notary.schedule;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.notary.entity.Custom;
import com.notary.mapper.CustomMapper;
import com.notary.service.CustomService;
import com.notary.util.DesUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
@Slf4j
public class ServerTask {

    @Value("${office.deskey}")
    private String deskey;
    @Autowired
    CustomService customService;

    //一个小时执行一次，获取机构信息
   // @Scheduled(cron = "0 0 0/1 * * ?")


  // @Scheduled(cron = "0 0/1 * * * ?")
    public void sendNotaryStatus() throws Exception{
        String result = HttpUtil.createPost("http://abc.yingnuoqian.cn/data-center-test/organService/getCustoms").header("Authorization", deskey)
                .body("", "application/json;utf-8").execute().body();
        JSONObject json = JSONObject.parseObject(result);
        String string= DesUtil.decode(deskey, json.getString("timestamp"), json.getString("data"));
        JSONObject object= JSON.parseObject(string);
        //0 失败 200 成功
        if(object.getString("code").equals("200")){
            log.info("获取机构信息接口成功:{}",string);
            JSONArray list= object.getJSONArray("data");
            if(CollectionUtil.isNotEmpty(list)){
                //数据插入
                List<Custom> customList= JSONUtil.toList(new cn.hutool.json.JSONArray(list),Custom.class);
                customService.saveBatch(customList);
            }

        }else{
        log.info("获取机构信息接口异常:{}",string);
        }
    }

}
